package club.yuit.desktop.ui;

import club.yuit.desktop.GlobalData;
import lombok.extern.slf4j.Slf4j;

import javax.swing.*;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
import java.awt.*;

/**
 * @author: yuit
 * @date: 2023/12/04 14:37
 */
@Slf4j
public class InitRMQAdminDialog {


    /**
     * name server 地址
     */
    private JTextField nameServer;

    /**
     * 代理地址
     */
    private JTextField proxyServer;

    private JTextField accessKey;
    private JTextField secretKey;


    private JComboBox<String> selectVersion;

    private JOptionPane opt;

    private JButton ok;
    private JButton cancel;

    private MainUI mainUI;


    public InitRMQAdminDialog(MainUI parent) {
        mainUI = parent;
        opt = new JOptionPane();
        nameServer = new JTextField("220.197.14.136:9879");
        proxyServer = new JTextField("10.0.0.110:10008");
        accessKey = new JTextField("");
        secretKey = new JTextField("");



        selectVersion = new JComboBox<>();
        selectVersion.addItem("5.X");
        selectVersion.addItem("4.X");
        opt.setMessage(new Object[]{
                new JLabel("请选择版本"),
                selectVersion,
                new JLabel("请输入name server 地址"),
                nameServer,
                new JLabel("请输入代理地址"),
                proxyServer,
                new JLabel("accesskey"),
                accessKey,
                new JLabel("secretkey"),
                secretKey,
        });

        ok = new JButton("确认");

        cancel = new JButton("取消");

        opt.setOptions(new Object[]{
                ok
        });

        // 事件监听在显示之前，show 方法会阻塞
        ok.addActionListener(e -> {
            String proxy = proxyServer.getText();
            String nsv = nameServer.getText();

            String access = accessKey.getText();
            String secret = secretKey.getText();

            System.setProperty("accessKey",access);
            System.setProperty("secretKey",secret);

            String version = (String) selectVersion.getSelectedItem();

            if("5.X".equals(version)&&proxy.isEmpty()){
                proxy = nsv;
            }

            boolean b = mainUI.initRMQAdmin(version, proxy, nsv);
            if (b) {
                UITools.disposeParent(ok);
            }
        });

        cancel.addActionListener(e -> {
            UITools.disposeParent(cancel);
        });



    }


    public void showDialog(){
         JOptionPane.showOptionDialog(
                 mainUI,
                 opt.getMessage(),
                 "初始化",
                 JOptionPane.DEFAULT_OPTION,
                 JOptionPane.PLAIN_MESSAGE,
                 opt.getIcon(),
                 opt.getOptions(),
                 opt.getInitialValue()
         );
    }




}
